﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.IO;

public partial class admin_addTeacher : System.Web.UI.Page
{
    db_teacher teacher = new db_teacher();
    string teacherId = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Server.Execute("../inc/checkPurview.aspx");  // 此处用户权限验证

        if (!IsPostBack) {
            teacherId = teacher.getNewTeacherId();
            tb_id.Text = teacherId;

            tb_picUrl.Text = "/admin/images/teacher.gif";
            img_pic.ImageUrl = "~/admin/images/teacher.gif";
            //ddl_grade.DataSource = new db_grade().allList().Tables[0].DefaultView;
            //ddl_grade.DataTextField = "grade";
            //ddl_grade.DataValueField = "grade";
            //ddl_grade.DataBind();
            //ddl_grade.Items.Insert(0, new ListItem("请选择年级", ""));
        }
    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (Session["adminName"] == null)
        {
            Response.Write("<script language=javascript>alert('没有权限！');</script>");
            return;
        }
        string teacherId = tb_id.Text;
        string teacherName = tb_name.Text;
        string about = tb_about.Text;
        string picUrl = tb_picUrl.Text;
        string pwd = "888888";
        if (teacher.addTeacher(teacherId, teacherName, about, picUrl, pwd))
        {
            res();
            Response.Write("<script language=javascript>alert('用户添加成功！');location.href='queryTeacher.aspx';</script>");
        }
        else
        {
            Response.Write("<script language=javascript>alert('失败！')</script>");
        }
    }
    protected void btn_res_Click(object sender, EventArgs e)
    {
        res();
    }

    private void res() {
        tb_name.Text = "";
        tb_about.Text="";
        tb_picUrl.Text="";
        img_pic.ImageUrl ="";
    }
    protected void btn_upload_Click(object sender, EventArgs e)
    {
        try
        {
            if (fud_img.PostedFile.FileName == "")
            {
                Response.Write("<script language=javascript>alert('请选择上传文件！');</script>");
                return;
            }
            else if (tb_id.Text == "") {
                Response.Write("<script language=javascript>alert('请先填写学号，再上传个人照片！');</script>");
                tb_id.Focus();
                return;
            }
            else
            {
                string filepath = fud_img.PostedFile.FileName;  //得到的是文件的完整路径,包括文件名，如：C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
                if (Tools.IsAllowedExtension(fud_img))
                {
                    if (fud_img.FileContent.Length / 1024 < 1000)//检查图片大小
                    {
                        string fileExt = filepath.Substring(filepath.LastIndexOf("."));//.jpg 
                        String newFileName = tb_id.Text + fileExt;
                        string serverpath = Server.MapPath("~/upload/teacherPic/");//取得文件在服务器上保存的位置C:\Inetpub\wwwroot\WebSite1\images\20022775_m.jpg 
                        if (!Directory.Exists(serverpath))
                        {
                            Directory.CreateDirectory(serverpath);  // 复制过来的代码改了这里，自动创建目录
                        }
                        fud_img.PostedFile.SaveAs(serverpath + newFileName);//将上传的文件另存为 
                        tb_picUrl.Text = "/upload/teacherPic/" + newFileName;
                        img_pic.ImageUrl ="~/upload/teacherPic/" + newFileName;
                        Response.Write("<script language=javascript>alert('上传成功！');</script>");
                    }
                    else
                    {
                        Response.Write("<script language=javascript>alert('上传图片文件过大，请上传小于1M的图片文件');</script>");
                    }
                }
                else
                {
                    Response.Write("<script language=javascript>alert('上传文件格式不正确！必须为：.jpg,.gif,bmp,png格式');</script>");
                }
            }
        }
        catch
        {
            Response.Write("<script language=javascript>alert('上传发生错误！');</script>");
        }
    }
}